package com.haoshuang.zhouzhoubang.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.annotation.ColorRes
import com.blankj.utilcode.util.SizeUtils
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.LayoutClickableEntryViewBinding
import com.mufeng.libs.utils.view.margin
import com.mufeng.libs.utils.view.textColor
import com.mufeng.libs.utils.view.visibleOrGone


/**
 * 自定义带有左边、右边图标，左边、右边文字的组合控件
 */
class ClickableEntryView(context: Context, attrs: AttributeSet? = null):
    LinearLayout(context, attrs) {
        init {
            init1(context, attrs)
        }

    // 标题
    private var title: String = ""

    // 标题字体大小
    private var titleSize: Float = 0.0f

    // 标题字体颜色
    private var titleColor = resources.getColor(R.color.c_33)


    // 提示语
    private var hint: String = ""

    // 内容
    private var text: String = ""

    // 内容颜色
    private var valueColor = resources.getColor(R.color.c_ff0000)

    // 左边图标
    private var leftIcon: Int? = null

    // 左边图标左边距
    private var leftIconMarginLeft: Float = 0.0f

    // 标题左边距
    private var titleMarginLeft: Float = 0.0f

    // 右边图标右边距
    private var rightIconMarginRight: Float = 0.0f

    // 右侧按钮显示/隐藏
    private var showRightIcon: Boolean = true

    private lateinit var binding: LayoutClickableEntryViewBinding

    @SuppressLint("ResourceAsColor")
    private fun init1(context: Context, attrs: AttributeSet?) {
        // 获取可配置的属性  设置给控件
        val attr = context.obtainStyledAttributes(attrs, R.styleable.ClickableEntryView)
        // 标题
        title = attr.getString(R.styleable.ClickableEntryView_entry_title) ?: ""
        titleSize = attr.getDimension(R.styleable.ClickableEntryView_entry_title_size,0.0f) ?: 0.0f
        titleColor = attr.getResourceId(R.styleable.ClickableEntryView_entry_title_color, R.color.c_33) ?: valueColor
        hint = attr.getString(R.styleable.ClickableEntryView_entry_hint) ?: ""
        text = attr.getString(R.styleable.ClickableEntryView_entry_text) ?: ""
        valueColor = attr.getResourceId(R.styleable.ClickableEntryView_entry_text_color, R.color.c_ff0000) ?: valueColor
        leftIcon = attr.getResourceId(R.styleable.ClickableEntryView_entry_left_icon, R.drawable.ic_launcher_background)
        leftIconMarginLeft = attr.getDimension(R.styleable.ClickableEntryView_entry_icon_marginLeft,0.0f) ?: 0.0f
        titleMarginLeft = attr.getDimension(R.styleable.ClickableEntryView_entry_title_marginLeft,0.0f) ?: 0.0f
        rightIconMarginRight = attr.getDimension(R.styleable.ClickableEntryView_entry_rightIcon_marginRight,0.0f) ?: 0.0f
        showRightIcon = attr.getBoolean(R.styleable.ClickableEntryView_entry_rightIcon_show, true)
        attr.recycle()
        val root = inflate(context, R.layout.layout_clickable_entry_view, this)
        binding = LayoutClickableEntryViewBinding.bind(root)
        initView1()

    }

    /**
     * 设置View
     */
    private fun initView1() {
        binding.apply {
            tvEntryTitle.text = title
            if (titleSize > 0.0f) tvEntryTitle.textSize = SizeUtils.px2dp(titleSize).toFloat()  //设置的是dp因此获取到的是转换后的px所以要进行转换
            if (leftIconMarginLeft > 0.0f){
                //设置的是dp因此获取到的是转换后的px所以要进行转换
                ivIcon.margin(SizeUtils.px2dp(leftIconMarginLeft)*2,0,0,0 )
            }
            tvEntryValue.text = text
            tvEntryValue.hint = hint
            leftIcon?.let {
                if (leftIcon != R.drawable.ic_launcher_background) ivIcon.setImageResource(it)
            }

            if (titleMarginLeft > 0.0f){
                //设置的是dp因此获取到的是转换后的px所以要进行转换
                tvEntryTitle.margin(SizeUtils.px2dp(titleMarginLeft)*2,0,0,0 )
            }
            if (rightIconMarginRight > 0.0f){
                //设置的是dp因此获取到的是转换后的px所以要进行转换
                ivRightArrow1.margin(0,0,SizeUtils.px2dp(rightIconMarginRight)*2,0 )
            }
        }
        binding.tvEntryValue.textColor = valueColor
        binding.tvEntryTitle.textColor = titleColor
        binding.ivRightArrow1.visibleOrGone(showRightIcon)
    }

    /**
     * 设置标题
     * @param title String
     */
    fun setTitle(title: String) {
        this.title = title
        binding.tvEntryTitle.text = title
    }

    fun getTitle() = title

    /**
     * 设置提示语
     * @param hint String
     */
    fun setHint(hint: String) {
        this.hint = hint
        binding.tvEntryValue.hint = hint
    }

    fun getHint() = hint

    /**
     * 设置值
     * @param text String
     */
    fun setText(text: String) {
        this.text = text
        binding.tvEntryValue.text = text
    }

    fun getText() = binding.tvEntryValue.text.toString()

    /**
     * 设置Value的字体颜色
     * @param colorRes String
     */
    fun setValueColor(@ColorRes colorRes: Int) {
        valueColor = colorRes
        binding.tvEntryValue.textColor = valueColor
    }

    /**
     * 设置条目值的点击事件
     * @param clickListener 点击监听
     */
   /* fun setValueClickListener(clickListener: OnClickListener){
        binding.tvEntryValue.setOnClickListener(clickListener)
    }*/


}